/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model;

import xperiment.metaphor.model.entity.Person;
import metaphor.service.*;
import metaphor.service.SecurityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
import org.springframework.security.acls.MutableAclService;
import org.springframework.security.providers.ProviderManager;
import xperiment.metaphor.model.entity.Department;
import xperiment.metaphor.model.entity.Organization;
import xperiment.metaphor.model.entity.Employee;
import xperiment.metaphor.model.entity.JobDescription;
import xperiment.metaphor.model.entity.Position;
import xperiment.metaphor.model.system.Message;
import xperiment.metaphor.model.entity.AuthenticationEvent;
import xperiment.metaphor.model.entity.AuthorizationEvent;
import xperiment.metaphor.model.entity.*;
import xperiment.metaphor.model.entity.Role;
import xperiment.metaphor.model.entity.User;
import xperiment.metaphor.model.notebook.Notebook;
import xperiment.metaphor.model.security.ACLEntry;
import xperiment.metaphor.model.security.ACLObjectId;
import xperiment.metaphor.model.security.ACLClass;
import xperiment.metaphor.model.security.ACLSecurityId;
import metaphor.persistence.Dao;
/**
 *
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 */
@ContextConfiguration(locations={"classpath:xperiment/metaphor/model/context-service.xml"})
public class AbstractSpringJUnit extends AbstractJUnit38SpringContextTests {
    /* 
     * This variable is meant to hide the JCL logger declared in the parent 
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
            
    public AbstractSpringJUnit(String testName) {
        super(testName);
    }            

    @Override
    protected void setUp() throws Exception {
        super.setUp();    
        Dao dao = getDao();

        /* Delete the ACLs*/
        dao.delete(dao.retrieve(ACLEntry.class));
        dao.delete(dao.retrieve(ACLObjectId.class));
        dao.delete(dao.retrieve(ACLClass.class));
        dao.delete(dao.retrieve(ACLSecurityId.class));
        
        dao.delete(dao.retrieve(Notebook.class));

        dao.delete(dao.retrieve(Person.class));
        dao.delete(dao.retrieve(Employee.class));
        dao.delete(dao.retrieve(User.class));

        dao.delete(dao.retrieve(Role.class));
        dao.delete(dao.retrieve(AuthenticationEvent.class));
        dao.delete(dao.retrieve(AuthorizationEvent.class));
        dao.delete(dao.retrieve(Message.class));

        dao.delete(dao.retrieve(Position.class));
        dao.delete(dao.retrieve(Department.class));
        dao.delete(dao.retrieve(Company.class));
        dao.delete(dao.retrieve(Organization.class));

        dao.delete(dao.retrieve(JobDescription.class));

        
        dao.delete(dao.retrieve(Gender.class));
        dao.delete(dao.retrieve(PopulationGroup.class));
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    protected ProviderManager getAuthenticationService() {
        return (ProviderManager) applicationContext.getBean("_authenticationManager");
    }

    protected MutableAclService getSecurityService() {
        return (MutableAclService) applicationContext.getBean(BeanNames.ACL_SERVICE, MutableAclService.class);
    }

    protected ApplicationManager getApplicationManager() {
        return (ApplicationManager) applicationContext.getBean(BeanNames.APPLICATION_MANAGER, ApplicationManager.class);
    }
    protected Object getBean(String name) throws ServiceException {
        return getApplicationManager().getBean(name);
    }
    protected Object getBean(String name, Class beanClass) throws ServiceException {
        return getApplicationManager().getBean(name, beanClass);
    }
    protected Dao getDao() throws ServiceException {
        return (Dao) getBean(BeanNames.DAO, Dao.class);
    }
    protected SecurityManager getSecurityManager() throws ServiceException {
        return (SecurityManager) getBean(BeanNames.SECURITY_MANAGER, SecurityManager.class);
    }
}
